package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisConstant;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Setmeal;
import com.atguigu.service.SetmealService;
import com.atguigu.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

/**
 * SetmealController
 *
 * @Author: 马伟奇
 * @CreateTime: 2021-01-30
 * @Description:
 */
@RequestMapping("/setmeal")
@RestController
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
       PageResult pageResult =  setmealService.findPage(queryPageBean);
       return pageResult;
    }


    @RequestMapping("/add")
    public Result add(Integer[] travelgroupIds, @RequestBody Setmeal setmeal){
        setmealService.add(travelgroupIds,setmeal);
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile) throws Exception{
        // 获取文件的名字 abc.jpg
        String originalFilename = imgFile.getOriginalFilename();
        // 找到图片名字里面的点
        int lastIndexOf = originalFilename.lastIndexOf(".");
        // 找到后缀
        String substring = originalFilename.substring(lastIndexOf);


        // 随机生成UUID,拼接URL地址  xxxx.jpg
        String fileName = UUID.randomUUID().toString() + substring;



        // 传文件
        QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
        // 往七牛云存成功之后，顺便往redis里面存图片
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);


        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);

    }
}